<template>
  <dir>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>
        <a href>商品管理</a>
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card v-loading="loading">
      <!-- 行     gutter是格式这里的间隔-->
      <el-row :gutter="20">
        <!-- 列 -->
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="GoodsList">
            <el-button slot="append" icon="el-icon-search" @click="GoodsList"></el-button>
          </el-input>
        </el-col>
        <!-- 列 -->
        <el-col :span="4">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>
            <!-- 用户列表区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="name" ></el-table-column>
        <el-table-column label="商品价格(元)" prop="price" width="100px"></el-table-column>
        <el-table-column label="库存状态" prop="stock" width="90px"></el-table-column>
        <el-table-column label="商品分类" prop="category" width="90px"></el-table-column>

          <el-table-column label="使用优惠券" width="90px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-switch v-model="scope.row.coupon" @change="couponStateChange(scope.row)"></el-switch>
          </template>
        </el-table-column>

        <el-table-column label="创建时间" prop="createTime" width="140px">
            <template slot-scope="scope">
                {{scope.row.createTime | dateFormat}}
            </template>
        </el-table-column>

        <el-table-column label="上架状态" width="90px">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-switch v-model="scope.row.upper_shelf" @change="upperShelfStateChange(scope.row)"></el-switch>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="130px">
          <!-- 作用域插槽 -->
          <!-- slot-scope="scope" -->
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row._id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoodsById(scope.row._id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </dir>
</template>
<script>
import { getGoodsList, delGoodsByid, updateCouponState, updateGoodsUpperShelfState } from '../../api/goods/list.js'
export default {
  data() {
    return {
      loading: false,
      queryInfo: {
        query: null,
        // 当前的页数
        pageNum: 1,
        // 当前每页显示多少条
        pageSize: 5
      },
      goodsList: [],
      total: 0
    }
  },
  created() {
    this.GoodsList()
  },
  methods: {
    // 获取商品列表
    GoodsList() {
      this.loading = true
      console.log(this.queryInfo)
      getGoodsList(this.queryInfo).then(res => {
        console.log(res.data)
        if (!res.errcode === 0) return this.$message.error('获取商品列表失败')
        this.goodsList = res.data.data
        this.total = res.data.Total
        // console.log('total' + this.total)
        this.loading = false
      })
    },
    // 监听 pageSize 改变事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pageSize = newSize
      this.GoodsList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      //   console.log(newPage)
      this.queryInfo.pageNum = newPage
      //   console.log(this.queryInfo.pageNum)
      this.GoodsList()
    },
    removeGoodsById(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该商品, 是否继续?', '删除商品提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(res => {
        console.log(res)
        if (res !== 'confirm') return
        this.loading = true
        delGoodsByid(id).then(res => {
          console.log(res.data)
          this.loading = false
          if (res.data.deleted === 0) return this.$message.error('删除商品失败')
          // 重新获取用户列表
          this.GoodsList()
          this.$message.success('删除指定商品完成')
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    goAddPage () {
      this.$router.push('/goods/add')
    },
    // 可否使用优惠券开关事件
    couponStateChange (item) {
      // console.log(item._id)
      // console.log(item.coupon)
      const params = {
        id: item._id,
        couponState: item.coupon
      }
      updateCouponState(params).then(res => {
        // console.log(res.data.data.modified)
        if (res.data.data.modified > 0) {
          this.$message.success('优惠券状态更新成功')
        }
      })
    },
    // 更新商品上下架状态
    upperShelfStateChange (item) {
      console.log(item._id)
      console.log(item.coupon)
      const params = {
        id: item._id,
        UpperShelfState: item.upper_shelf
      }
      updateGoodsUpperShelfState(params).then(res => {
        // console.log(res)
        if (res.data.data.modified > 0) {
          this.$message.success('商品上架状态更新成功')
        }
      })
    },
    // 修改商品功能页面跳转
    showEditDialog (id) {
      console.log(id)
      this.$router.push('/goods/update/?id=' + id)
    }
  }
}
</script>
<style lang="less" scoped>
</style>
